<?php
// 首页显示信息以及基本配置
namespace Partner\Controller;
use Think\Controller;
class TeamController extends CommonController{
   function team_list(){
       $p_id=session('id');
       $user_info=M('partner')->find($p_id);
	   /* if($user_info['partent_id']!=0){
		   $user_list2=M('partner')
			   ->field('yt_users.phone,yt_users.username,yt_partner.*')
			   ->join('yt_users on yt_partner.u_id=yt_users.id')
			   ->where('partent_id='.$user_info['partent_id'])->select();
		   $this->assign('user_list2',$user_list2);
	   } */
        $user_list=M('partner')
            ->field('yt_users.phone,yt_users.username,yt_partner.*')
           ->join('yt_users on yt_partner.u_id=yt_users.id')
            ->where('partent_id='.$p_id)->select();
        $this->assign('user_list',$user_list);
        $this->display();
    }
    function teams_add(){
        $p_id=session('id');
        $user_info=M('partner')->find($p_id);
        if($_GET['id']){
            $g_id=I('get.id');
            $rs_p=M('partner')->find($g_id);
            $this->assign('rs_p',$rs_p);
        }
        $this->assign('user_info',$user_info);
        $this->display();
    }
    function teams_add_post(){
        $p_id=session('id');
       if($_POST){
           $data=I('post.');
           if($data['id']==null){
               $user_rs=M('partner')->where('u_id='.$data['u_id'])->find();
               if($user_rs){
                   $this->ajaxReturn(4);
               }
           }

           if($data['status']==null){
               $data['status']=1;
           }
           $info['u_id']=$data['u_id'];
           $info['level']=$data['level'];
           $info['b_id']=$data['b_id'];
           $info['status']=$data['status'];
           $info['order_edit']=$data['order_edit'];
           $info['partent_id']=$p_id;
           if($data['id']){
               $info['id']=$data['id'];
               $rs=M('partner')->save($info);
               $this->ajaxReturn(2);
           }else{
               $rs=M('partner')->add($info);
               if($rs){
                   $this->ajaxReturn(1);
               }else{
                   $this->ajaxReturn(0);
               }
           }


       }
    }

    public function search_phone2(){
        $phone=I('get.phone');
        $users=M('users');
        $rs=$users->where('phone='.$phone)->find();
        if($rs){
            $partner=M('partner');
            $c_rs=$partner->where('u_id='.$rs['id'])->select();
            if($c_rs){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn($rs);
            }
        }else{
            $this->ajaxReturn(0);
        }

    }

    function authority(){
        $menu   =   D('Menu');
        $this->assign('title','权限配置');
		$id=I('get.id');
		$where['uid']=$id;
        $where['type']=2;
        $rs=M('rbac')->where($where)->find();
        
        $this->assign('rs',$rs);
		
		$p_id=session('id');
        $user_info=M('partner')->find($p_id);
		if($user_info['partent_id']==0){
			//城市管理员（属后台任命，没有上级）
			$menudatashowArray = $menu->where('type = 1')->order('sort ASC')->select();
		}else{
			$where_head['uid']=$p_id;
	        $where_head['type']=2;
			$rs_head=M('rbac')->where($where_head)->find();
			$menudatashowArray = $menu->where('type = 1 and id IN ('.$rs_head['bids'].')')->order('sort ASC')->select();
		}
        $new_data=getTree($menudatashowArray,0);
        $this->assign('new_data',$new_data);
      
        $this->display();
    }

    function  edit_rbac(){
        if($_POST){
            $data=I('post.');
            $where['uid']=$data['uid'];
            $where['type']=2;
            $rs=M('rbac')->where($where)->find();
            if($rs){
                $eidt_where['bids']=$data['adIds'];
                $eidt_where['id']=$rs['id'];

                $result=M('rbac')->save($eidt_where);
            }else{
                $where['bids']=$data['adIds'];
                $result=M('rbac')->add($where);
            }
            if($result){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }
    }

}